const app = getApp();

Page({
  data: {
    outline: false,
    hidden: false,
    login: 0,
    imgUrl: '',
    nickName: '',
    isshare: '',
    openid: ''
  },
  onLoad(query) {
    // 页面加载


  },
  onReady() {
    // 页面加载完成
  },

  goodsOrder: function() {
    my.navigateTo({
      url: '/pages/goodsOrder/goodsOrder'
    })

  },
  allOrder: function() {
    my.navigateTo({
      url: '/pages/order/order'
    })

  },
  myAddress: function() {
    my.navigateTo({
      url: '/pages/myAddress/myAddress',
    })
  },

  onShow() {
    var that = this;
    my.getNetworkType({
      success: (res) => {
        if (res.networkAvailable) {
          that.setData({
            outline: false,
            todos: []
          })
          // 获取token
          my.getStorage({
            key: 'token',
            success: function(res) {
              that.setData({
                token: res.data
              })
            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          //获取openid
          my.getStorage({
            key: 'openid',
            success: function(res) {
              that.setData({
                openid: res.data,
              })

            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          my.getStorage({
            key: 'imgUrl',
            success: function(res) {
              if (res.data == null) {
                that.setData({
                  login: 0
                })
              } else {
                that.setData({
                  imgUrl: res.data
                })
              }

            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
          my.getStorage({
            key: 'nickName',
            success: function(res) {
              if (res.data == null) {
                that.setData({
                  login: 0
                })
              } else {
                that.setData({
                  nickName: res.data,
                  login: 1
                })
              }

            },
            fail: function(res) {
              my.alert({ content: res.errorMessage });
            }
          })
        } else {
          that.setData({
            outline: true
          })
        }
      }
    })
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  bindKeyInput(e) {
    this.setData({
      code: e.detail.value
    });
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
  // 收货地址 ,获取到useid
  get() {
    var that = this;
    my.getAuthCode({
      scopes: 'auth_user',
      success: (res) => { //获取用户信息
        my.getAuthUserInfo({
          success: (res) => {
            var req = {
              openid: that.data.openid,
              nickname: res.nickName,
              headimgurl: res.avatar,
              sex: '',
              province: '',
              city: '',
            }
            my.request({ // 调用自己的服务端接口，让服务端进行后端的授权认证，并且种session，需要解决跨域问题
              url: 'https://www.tiantong369.com/crm/myuserinfo', // 该url是自己的服务地址
              method: 'POST',
              data: req,
              headers: {
                'x-access-token': that.data.token
              },
              success: (res) => {// 成功获取订单号
                if (res.data.status == 200) {
                  that.setData({
                    imgUrl: res.data.data.headimgurl,
                    nickName: res.data.data.nickname,
                    login: 1
                  })
                  my.setStorageSync(
                    {
                      key: "imgUrl",
                      data: res.data.data.headimgurl
                    }
                  );
                  my.setStorageSync(
                    {
                      key: "nickName",
                      data: res.data.data.nickname
                    }
                  );
                  my.setStorageSync(
                    {
                      key: "userId",
                      data: res.data.data.id
                    }
                  );

                } else {
                  my.showToast({
                    content: '' + res.data.message + '',
                  })
                }
              },
              fail: (res) => {
                console.log('/Wxpay_callback:', res)
              },
            });
          },
        });

      },
    })
  },
  // 获取个人信息 支付宝登录
  str() {
    var that = this;
    that.setData({
      hidden: true
    })
    my.getAuthCode({

      scopes: 'auth_user',

      success: (res) => { //获取用户信息

        my.getAuthUserInfo({
          success: (res) => {
            my.setStorageSync(
              {
                key: "imgUrl",
                data: res.avatar,
              }
            );

            my.setStorageSync(
              {
                key: "nickName",
                data: res.nickName
              }
            );
            that.setData({
              nickName: res.nickName,
              imgUrl: res.avatar,
              login: 1,
              hidden: false
            })
          }

        });

      },
    });
  }
});
